package com.example.myandroidtest.ui.media;

import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myandroidtest.databinding.ActivityMediaCodecBinding;

import java.io.IOException;

public class MediaCodecActivity extends AppCompatActivity {

    ActivityMediaCodecBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        binding = ActivityMediaCodecBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        binding.toolbar.ivBack.setOnClickListener(v -> finish());
        binding.toolbar.tvTitle.setText("MediaCodec");

        binding.btnCodecList.setOnClickListener(v -> listCodecList());
    }

    //列出手机编译器
    private void listCodecList() {
        //ALL_CODECS 表示列出所有编解码器
        //REGULAR_CODECS 表示只列出正常编解码器
        MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
        MediaCodecInfo[] codecInfos = codecList.getCodecInfos();
        for (MediaCodecInfo codecInfo : codecInfos) {

            String name = codecInfo.getName(); // 编解码器名称
            boolean isEncoder = codecInfo.isEncoder(); // 是编码器吗？
            String[] types = codecInfo.getSupportedTypes(); // 支持的媒体类型
            for (String type : types) {
                MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(type); // 编解码器的配置参数
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                String canonicalName = codecInfo.getCanonicalName(); // 编解码器规范名称
                boolean isVendor = codecInfo.isVendor(); // 是厂商提供的吗？
                boolean isSoftwareOnly = codecInfo.isSoftwareOnly(); // 纯软件实现的编解码器？
                boolean isHardwareAccelerated = codecInfo.isHardwareAccelerated(); // 有硬件加速？
                System.out.println("canonicalName:" + canonicalName + " isVendor:" + isVendor + " isSoftwareOnly:" + isSoftwareOnly + " isHardwareAccelerated:" + isHardwareAccelerated);
            }
            System.out.println("name:" + name + " isEncoder:" + isEncoder + " types:" + types);
        }

        boolean isEncoder = true; // 是否为编码器
        String mimeType = "video/avc"; // 媒体类型
        MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, 640, 480);
        String codecInfo = codecList.findEncoderForFormat(format);
        System.out.println("codecInfo:" + codecInfo);
    }


    //解码
    void onDecode() throws IOException {
        String mimetype = MediaFormat.MIMETYPE_VIDEO_AVC;
        MediaFormat format = MediaFormat.createVideoFormat(mimetype, 640, 480);
        MediaCodec decoder = MediaCodec.createDecoderByType(mimetype);
        decoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        decoder.start();
    }

    //编码
    void onEncode() throws IOException {
        String mimetype = MediaFormat.MIMETYPE_VIDEO_AVC;
        MediaFormat format = MediaFormat.createVideoFormat(mimetype, 640, 480);
        MediaCodec encoder = MediaCodec.createEncoderByType(mimetype);
        encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        encoder.start();
        //app 编码
    }
}